package il.ac.tau.cg.ex3.parser.parametershander.surfaces;

import il.ac.tau.cg.ex3.entities.scene.Scene;
import il.ac.tau.cg.ex3.entities.styles.Material;
import il.ac.tau.cg.ex3.entities.surfaces.RectangleSurface;
import il.ac.tau.cg.ex3.math.Point;
import il.ac.tau.cg.ex3.math.shapes.Rectangle;
import il.ac.tau.cg.ex3.parser.Parser;
import il.ac.tau.cg.ex3.parser.Parser.ParseException;

public class RectangleParameterHandler extends SurfaceParameterHandler
{
	private Point p0;
	private Point p1;
	private Point p2;
	
	public RectangleParameterHandler()
	{
		super();
		
		p0 = null;
		p1 = null;
		p2 = null;
	}
	
	public boolean addParam(String key, String[] value) throws ParseException
	{
		if (super.addParam(key, value))
		{
			return true;
		}
		
		ESurfaceParameters param =  ESurfaceParameters.findParam(key);
		
		if (param == null)
		{
			return false;
		}
		
		Parser.pAssert((value.length == param.argsNum), param.name + " INVALID ARGS NUM");
							
		switch (param)
		{
			case P0:
			{
				p0 = getPoint(value);
			}break;
			
			case P1:
			{
				p1 = getPoint(value);
			}break;
			
			case P2:
			{
				p2 = getPoint(value);
			}break;
		}
		
		return true;
	}
	
	protected void createEntity(Scene scene) throws ParseException
	{
		scene.addSurface(new RectangleSurface((Material)super.createEntity(), new Rectangle(p0, p1, p2)));
	}

	@Override
	protected boolean validateEntity() throws ParseException
	{
		for (ESurfaceParameters param : ESurfaceParameters.values())
		{
			if (param.isMandatory && !paramsCache.contains(param.name))
			{
				return false;
			}
		}
		
		return true;
	}
	
	private enum ESurfaceParameters
	{
		/* RECTANGLE and BOX ENTITIES */
		P0 ("p0", true, 3),
		P1 ("p1", true, 3),
		P2 ("p2", true, 3);
		
		private final String  name;
		private final boolean isMandatory;
		private final int     argsNum;
		
		ESurfaceParameters(String name, boolean isMandatory, int argsNum)
		{
			this.name = name;
			this.isMandatory = isMandatory;
			this.argsNum = argsNum;
		}
		
		private static ESurfaceParameters findParam(String name)
		{
			for (ESurfaceParameters param : ESurfaceParameters.values())
			{
				if (param.name.equals(name))
				{
					return param;
				}
			}
			
			return null;
		}
	}
}

